package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarBoxNoticeData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarCampData;
import com.yanqu.road.entity.unioncampwar.UnionCampWarNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarBoxNoticeDaoImpl extends TempDataObjectDao<UnionCampWarBoxNoticeData> {

    public static String getUnionCampWarBoxNoticeDataSql = "select * from `t_c_union_camp_war_box_notice` where `weekId`=?";

    public Map<Integer, Map<Integer, List<UnionCampWarBoxNoticeData>>> getUnionCampWarBoxNoticeDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<UnionCampWarBoxNoticeData>>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarBoxNoticeDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarBoxNoticeData data = parse(rs);
                Map<Integer, List<UnionCampWarBoxNoticeData>> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (null == centerGroupMap) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                List<UnionCampWarBoxNoticeData> dataList = centerGroupMap.get(data.getGroupId());
                if (dataList == null) {
                    dataList = new ArrayList<>();
                    centerGroupMap.put(data.getGroupId(), dataList);
                }
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean updateData(UnionCampWarBoxNoticeData unionCampWarBoxNoticeData) {
        return false;
    }

    public static String insertUnionCampWarBoxNoticeDataSql = "insert into `t_c_union_camp_war_box_notice`(centerGroupId,groupId,weekId,campId,userId,sererId,userName,rewards,openTime) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionCampWarBoxNoticeData unionCampWarBoxNoticeData) {
        unionCampWarBoxNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarBoxNoticeData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarBoxNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarBoxNoticeData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarBoxNoticeData.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarBoxNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarBoxNoticeData.getSererId()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarBoxNoticeData.getUserName()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarBoxNoticeData.getRewards()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarBoxNoticeData.getOpenTime()));
        if (getDb().executeNoneQuery(insertUnionCampWarBoxNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarBoxNoticeData parseData(ResultSet rs) throws SQLException {
        UnionCampWarBoxNoticeData unionCampWarBoxNoticeData = new UnionCampWarBoxNoticeData();
        unionCampWarBoxNoticeData.setCenterGroupId(rs.getInt("centerGroupId"));
        unionCampWarBoxNoticeData.setGroupId(rs.getInt("groupId"));
        unionCampWarBoxNoticeData.setWeekId(rs.getInt("weekId"));
        unionCampWarBoxNoticeData.setCampId(rs.getInt("campId"));
        unionCampWarBoxNoticeData.setUserId(rs.getLong("userId"));
        unionCampWarBoxNoticeData.setSererId(rs.getLong("sererId"));
        unionCampWarBoxNoticeData.setUserName(rs.getString("userName"));
        unionCampWarBoxNoticeData.setRewards(rs.getString("rewards"));
        unionCampWarBoxNoticeData.setOpenTime(rs.getLong("openTime"));

        return unionCampWarBoxNoticeData;
    }
}
